﻿/******************************************************************************
* SunnyUI.FrameDecoder 开源TCP、串口数据解码库。
* CopyRight (C) 2022-2023 ShenYongHua(沈永华).
* QQ群：56829229 QQ：17612584 EMail：SunnyUI@qq.com
*
* Blog:   https://www.cnblogs.com/yhuse
* Gitee:  https://gitee.com/yhuse/SunnyUI.FrameDecoder
*
* SunnyUI.FrameDecoder.dll can be used for free under the MIT license.
* If you use this code, please keep this note.
* 如果您使用此代码，请保留此说明。
******************************************************************************
* 文件名称: DecoderDefine.cs
* 文件说明: 数据帧解码器定义类
* 当前版本: V1.0
* 创建日期: 2022-11-01
*
* 2022-11-01: V1.0.0 增加文件说明
******************************************************************************/

using System;

namespace Sunny.FrameDecoder
{
    /// <summary>
    /// 解码错误事件类
    /// </summary>
    public class DecoderErrorEventArgs : EventArgs
    {
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; }

        /// <summary>
        /// 当前数据
        /// </summary>
        public byte[] ByteData { get; }

        /// <summary>
        /// 当前数据
        /// </summary>
        public string StringData { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="data">当前数据</param>
        public DecoderErrorEventArgs(string message, byte[] data)
        {
            Message = message;
            ByteData = data;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="data">当前数据</param>
        public DecoderErrorEventArgs(string message, string data)
        {
            Message = message;
            StringData = data;
        }
    }

    /// <summary>
    /// 解码错误事件类
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">解码错误事件类</param>
    public delegate void OnDecoderError(object sender, DecoderErrorEventArgs e);

    /// <summary>
    /// 字节数组数据接口
    /// </summary>
    public interface IByteEventArgs
    {
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Value { get; }

        /// <summary>
        /// 解码器
        /// </summary>
        public BaseByteFrameDecoder Decoder { get; }
    }

    /// <summary>
    /// 字节数组数据事件类
    /// </summary>
    public class ByteEventArgs : EventArgs
    {
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Value { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value">数据</param>
        public ByteEventArgs(byte[] value)
        {
            Value = value;
        }
    }

    /// <summary>
    /// 字节数组数据事件类
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">字节数组数据事件类</param>
    public delegate void OnByteEventHandler(object sender, IByteEventArgs e);

    /// <summary>
    /// 字节数组解码数据事件类
    /// </summary>
    public class ByteDataEventArgs : ByteEventArgs, IByteEventArgs
    {
        /// <summary>
        /// 解码器
        /// </summary>
        public BaseByteFrameDecoder Decoder { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="value">数据</param>
        public ByteDataEventArgs(BaseByteFrameDecoder decoder, byte[] value) : base(value)
        {
            Decoder = decoder;
        }
    }

    /// <summary>
    /// 字节数组解码帧数据事件类
    /// </summary>
    public class ByteFrameDataEventArgs : ByteDataEventArgs
    {
        /// <summary>
        /// 整帧数据，包含分隔符、标签、数据头、数据尾部等
        /// </summary>
        public byte[] Body { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public ByteFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] value, byte[] body) : base(decoder, value)
        {
            Body = body;
        }
    }

    /// <summary>
    /// 数据头部、数据尾部字节数组解码帧数据事件类
    /// </summary>
    public class HeaderTailByteFrameDataEventArgs : ByteFrameDataEventArgs
    {
        /// <summary>
        /// 数据头部
        /// </summary>
        public byte[] Headers { get; }

        /// <summary>
        /// 数据尾部
        /// </summary>
        public byte[] Tails { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="headers">数据头部</param>
        /// <param name="tails">数据尾部</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public HeaderTailByteFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] headers, byte[] value, byte[] tails, byte[] body) : base(decoder, value, body)
        {
            Headers = headers;
            Tails = tails;
        }
    }

    /// <summary>
    /// 帧固定长度字节数组解码帧数据事件类
    /// </summary>
    public class FixedLengthByteFrameDataEventArgs : ByteDataEventArgs
    {
        /// <summary>
        /// 帧固定长度
        /// </summary>
        public int FrameLength { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="frameLength">帧固定长度</param>
        /// <param name="value">数据</param>
        public FixedLengthByteFrameDataEventArgs(BaseByteFrameDecoder decoder, int frameLength, byte[] value) : base(decoder, value)
        {
            FrameLength = frameLength;
        }
    }

    /// <summary>
    /// 分隔符字节数组解码帧数据事件类
    /// </summary>
    public class DelimiterByteFrameDataEventArgs : ByteFrameDataEventArgs
    {
        /// <summary>
        /// 分隔符
        /// </summary>
        public byte[] Delimiters { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="value">数据</param>
        /// <param name="delimiters">分隔符</param>
        /// <param name="body">整帧数据</param>
        public DelimiterByteFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] value, byte[] delimiters, byte[] body) : base(decoder, value, body)
        {
            Delimiters = delimiters;
        }
    }

    /// <summary>
    /// 数据头部分隔符字节数组解码帧数据事件类
    /// </summary>
    public class HeaderDelimiterByteFrameDataEventArgs : ByteFrameDataEventArgs
    {
        /// <summary>
        /// 数据头部分隔符
        /// </summary>
        public byte[] HeaderDelimiters { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="headers">数据头部分隔符</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public HeaderDelimiterByteFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] headers, byte[] value, byte[] body) : base(decoder, value, body)
        {
            HeaderDelimiters = headers;
        }
    }

    /// <summary>
    /// 字节数组解码数据事件类（增加长度）
    /// </summary>
    public class LengthValueFrameDataEventArgs : ByteFrameDataEventArgs
    {
        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="len">长度</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public LengthValueFrameDataEventArgs(BaseByteFrameDecoder decoder, int len, byte[] value, byte[] body) : base(decoder, value, body)
        {
            Length = len;
        }
    }

    /// <summary>
    /// CanOpen包模式
    /// </summary>
    public class CanOpenPackageFrameDataEventArgs : ByteFrameDataEventArgs
    {
        /// <summary>
        /// COBID
        /// </summary>
        public ushort COBID { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder"></param>
        /// <param name="value"></param>
        /// <param name="body"></param>
        public CanOpenPackageFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] value, byte[] body) : base(decoder, value, body)
        {
            COBID = (ushort)((body[6] << 8) + body[7]);
        }
    }

    /// <summary>
    /// 字节数组解码数据事件类（增加数据头部、标签、数据长度、数据、数据尾部）
    /// </summary>
    public class HeaderTagLengthValueTailFrameDataEventArgs : TagLengthValueFrameDataEventArgs
    {
        /// <summary>
        /// 数据头部
        /// </summary>
        public byte[] Headers { get; }

        /// <summary>
        /// 数据尾部
        /// </summary>
        public byte[] Tails { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="headers">数据头部</param>
        /// <param name="tag">标签</param>
        /// <param name="len">长度</param>
        /// <param name="value">数据</param>
        /// <param name="tails">数据尾部</param>
        /// <param name="body">整帧数据</param>
        public HeaderTagLengthValueTailFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] headers, byte[] tag, int len, byte[] value, byte[] tails, byte[] body) : base(decoder, tag, len, value, body)
        {
            Headers = headers;
            Tails = tails;
        }
    }

    /// <summary>
    /// 字节数组解码数据事件类（增加标签、长度）
    /// </summary>
    public class TagLengthValueFrameDataEventArgs : LengthValueFrameDataEventArgs
    {
        /// <summary>
        /// 标签
        /// </summary>
        public byte[] Tag { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="tag">标签</param>
        /// <param name="len">长度</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public TagLengthValueFrameDataEventArgs(BaseByteFrameDecoder decoder, byte[] tag, int len, byte[] value, byte[] body) : base(decoder, len, value, body)
        {
            Tag = tag;
        }
    }

    /// <summary>
    /// 字符串数据接口
    /// </summary>
    public interface IStringEventArgs
    {
        /// <summary>
        /// 数据
        /// </summary>
        public string Value { get; }

        /// <summary>
        /// 解码器
        /// </summary>
        public BaseStringFrameDecoder Decoder { get; }
    }

    /// <summary>
    /// 字符串数据事件类
    /// </summary>
    public class StringEventArgs : EventArgs
    {
        /// <summary>
        /// 数据
        /// </summary>
        public string Value { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value">数据</param>
        public StringEventArgs(string value)
        {
            Value = value;
        }
    }

    /// <summary>
    /// 字符串数据事件类
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">字符串解码数据事件类</param>
    public delegate void OnStringEventHandler(object sender, IStringEventArgs e);

    /// <summary>
    /// 字符串解码数据事件类
    /// </summary>
    public class StringDataEventArgs : StringEventArgs, IStringEventArgs
    {
        /// <summary>
        /// 解码器
        /// </summary>
        public BaseStringFrameDecoder Decoder { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="value">数据</param>
        public StringDataEventArgs(BaseStringFrameDecoder decoder, string value) : base(value)
        {
            Decoder = decoder;
        }
    }

    /// <summary>
    /// 字符串解码帧数据事件类
    /// </summary>
    public class StringFrameDataEventArgs : StringDataEventArgs
    {
        /// <summary>
        /// 整帧数据，包含分隔符、标签、数据头、数据尾部等
        /// </summary>
        public string Body { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="decoder">解码器</param>
        /// <param name="value">数据</param>
        /// <param name="body">整帧数据</param>
        public StringFrameDataEventArgs(BaseStringFrameDecoder decoder, string value, string body = "") : base(decoder, value)
        {
            Body = body;
        }
    }
}
